import { companylist } from "@/api/dataredeem/warehouse";
import { warehouseManagement_ListAPI } from "@/api/data/warehouse-management";
import { storeManagement_ListAPI } from "@/api/data/store-management";
import { itemCategory_ListAPI } from "@/api/data/item-category";
import { timely_maintenance_listAPI } from "@/api/data/timely-maintenance.js";
import { destinationMaintenance_ListAPI } from "@/api/data/destination-maintenance.js";
import { carrier_ListAPI } from "@/api/data/carrier.js";
import { customsClearanceCompany_ListAPI } from "@/api/data/customs-clearance-company";
import { getReginList } from '@/api/data/regin-maintenance'

const custom_dict = {
  namespaced: true,
  state: {
    company: [], // 公司
    warehouse: [], // 仓库
    shop: [], // 店铺
    timely: [], // 时效
    destination: [], // 目的地
    category: [], // 海外货品类型
    carrier: [], //承运商
    customCompany: [], // 清关公司
    area: [] // 地区
  },

  mutations: {
    SET_COMPANY(state, company) {
      state.company = company;
    },
    SET_WAREHOUSE(state, warehouse) {
      state.warehouse = warehouse;
    },
    SET_SHOP(state, shop) {
      state.shop = shop;
    },
    SET_CATEGORY(state, shop) {
      state.category = shop;
    },
    //时效
    SET_TIMELY(state, timely) {
      state.timely = timely;
    },
    // 目的地
    SET_DESTINATION(state, destination) {
      state.destination = destination;
    },
    // 承运商
    SET_CARRIER(state, carrier) {
      state.carrier = carrier;
    },
    // 清关公司
    SET_CUSTOM(state, customCompany) {
      state.customCompany = customCompany;
    },
    SET_AREA(state, area) {
      state.area = area;
    },
    CLEAR_DICT(state) {
      state.commany = [];
      state.warehouse = [];
    },
  },

  actions: {
    // 异步设置所有自定义字典仓库
    async setDict({ dispatch, commit, state }) {
      // 设置快递公司列表
      await dispatch("company");
      // 设置仓库
      await dispatch("warehouse");
      // 设置店铺
      await dispatch("shop");
      // 设置货物类型
      await dispatch("category");
      // 时效
      await dispatch("timely");
      // 目的地
      await dispatch("destination");
      // 承运商
      await dispatch("carrier");
      // 清关公司
      await dispatch("customsClearanceCompany");
      // 地区
      await dispatch("areas");
    },
    // 检查字典内容是否存在
    checkDict({ commit, state }) {
      let flag = false;
      if (state.company.length === 0) flag = true;
      if (state.warehouse.length === 0) flag = true;
      if (state.shop.length === 0) flag = true;
      if (state.timely.length === 0) flag = true;
      if (state.destination.length === 0) flag = true;
      if (state.category.length === 0) flag = true;
      if (state.carrier.length === 0) flag = true;
      if (state.customCompany.length === 0) flag = true;
      if (state.area.length === 0) flag = true;
      return flag;
    },
    // 设置快递公司列表
    async company({ commit, state }) {
      // 设置快递公司列表
      const companyRes = await companylist();
      commit("SET_COMPANY", companyRes.rows);
    },
    // 设置仓库
    async warehouse({ commit, state }) {
      const wareHouseRes = await warehouseManagement_ListAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_WAREHOUSE", wareHouseRes.rows);
    },
    // 设置店铺
    async shop({ commit, state }) {
      const shopRes = await storeManagement_ListAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_SHOP", shopRes.rows);
    },
    // 设置货物类型
    async category({ commit, state }) {
      const categoryRes = await itemCategory_ListAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_CATEGORY", categoryRes.rows);
    },
    // 时效
    async timely({ commit, state }) {
      const timelyRes = await timely_maintenance_listAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_TIMELY", timelyRes.rows);
    },
    // 目的地
    async destination({ commit, state }) {
      const destinationRes = await destinationMaintenance_ListAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_DESTINATION", destinationRes.rows);
    },
    // 承运商
    async carrier({ commit, state }) {
      const carrierRes = await carrier_ListAPI({ pageSize: 50, pageNum: 1 });
      commit("SET_CARRIER", carrierRes.rows);
    },
    // 清关公司
    async customsClearanceCompany({ commit, state }) {
      const customCompanyRes = await customsClearanceCompany_ListAPI({
        pageSize: 50,
        pageNum: 1,
      });
      commit("SET_CUSTOM", customCompanyRes.rows);
    },
    // 地区维护
    async areas({ commit, state }) {
      const areaRes = await getReginList()
      console.log(areaRes)
      commit("SET_AREA", areaRes.data);
    },
  },
};

export default custom_dict;
